package com.pn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pn.entity.Client;
import com.pn.entity.Store;
import com.pn.entity.Supply;
import com.pn.mapper.SupplyMapper;
import com.pn.service.SupplyService;
import com.pn.utils.PageBean;
import com.pn.utils.Result;
import com.pn.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class SupplyServiceImpl implements SupplyService {

    @Autowired
    private SupplyMapper supplyMapper;

    //显示供货商信息
    @Override
    public PageBean supplyPageList(Supply supply) {
        PageHelper.startPage(supply.getPageNum(),supply.getPageSize());
        Page<Supply> page = supplyMapper.supplyPageList(supply);
        return new PageBean(page.getTotal(),page.getResult());
    }

    //修改供货商
    @Override
    public Result supplyUpdate(Supply supply) {

        int i = supplyMapper.supplyUpdate(supply);
        if (i > 0 ){
            return Result.ok("修改供货商成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"修改供货商失败");
    }

    //添加供货商
    @Override
    public Result supplyAdd(Supply supply) {

        supply.setIsDelete(0);
        int i = supplyMapper.supplyInsert(supply);
        if (i > 0 ){
            return Result.ok("添加供货商成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"添加供货商失败");
    }

    //删除供货商
    @Override
    public Result supplyDelete(Integer supplyId) {

        int i = supplyMapper.supplyDelete(supplyId);
        if (i > 0 ){
            return Result.ok("删除供货商成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"删除供货商失败");
    }

    //显示供货商表
    @Override
    public List<Supply> supplyList() {
        List<Supply> supplyList =  supplyMapper.supplyList();
        return supplyList;
    }

    //查看供货商编号是否存在
    @Override
    public Result supplyNumCheck(String supplyNum1) {
        List<Supply> store = supplyMapper.supplyNumCheck(supplyNum1);
        if (store.size() != 0){
            return Result.err(Result.CODE_ERR_BUSINESS,"供货商编号已存在");
        }

        return Result.ok(true);
    }
}
